package com.rocksdata.mc.ui.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.ui.view.NoScrollViewPager;
import com.ctz.commonframe.ui.widget.ViewPagerFragmentAdapter;
import com.ctz.commonframe.utils.DateUtil;
import com.ctz.commonframe.utils.Logger;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.entity.WeekTimetableInfo;
import com.rocksdata.mc.ui.dialog.CourseDetailDialog;
import com.rocksdata.mc.ui.fragment.WeekTimetableFragment2;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.TimetableManager;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.CourseInfo;
import com.rocksdata.mc.entity.UserInfo;
import com.rocksdata.mc.ui.fragment.DayTimetableFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/2/13.
 */

@ContentView(R.layout.activity_timetable)
public class TimetableActivity extends TitleBarActivity {
    @ViewInject(R.id.vp_timetable)
    private NoScrollViewPager vpTimetable;

//    private List<TimetableInfo> timetableInfoList = new ArrayList<TimetableInfo>();
//    private List<TimetableInfo> todayTimetableInfoList = new ArrayList<TimetableInfo>();

//    private WeekTimetableInfo info;

    private DayTimetableFragment dayFragment;
    private WeekTimetableFragment2 weekFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBackgroundColor(getResources().getColor(R.color.light_black));
//        setTitleRightRes(R.mipmap.ic_drag_handle);
        View tab = LayoutInflater.from(this).inflate(R.layout.view_timetable_tab, null);
        final TextView tabLeft = (TextView) tab.findViewById(R.id.tab_timetable_left);
        final TextView tabRight = (TextView) tab.findViewById(R.id.tab_timetable_right);
        View.OnClickListener tabClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tab_timetable_left:
                        vpTimetable.setCurrentItem(0);
                        tabLeft.setSelected(true);
                        tabRight.setSelected(false);
                        break;
                    case R.id.tab_timetable_right:
                        vpTimetable.setCurrentItem(1);
                        tabLeft.setSelected(false);
                        tabRight.setSelected(true);
                        break;
                }
            }
        };
        tabLeft.setOnClickListener(tabClickListener);
        tabRight.setOnClickListener(tabClickListener);
        List<Fragment> fragmentList = new ArrayList<Fragment>();
        dayFragment = new DayTimetableFragment();
        fragmentList.add(dayFragment);
        weekFragment = new WeekTimetableFragment2();
        fragmentList.add(weekFragment);
        vpTimetable.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragmentList));
        tabLeft.performClick();
        setTitleMiddleView(tab);
        getWeekTimetableInfo();
    }

    private void getWeekTimetableInfo() {
//        showLoadingDialog();
//        BusinessInterface.OnBusinessResultListener listener = new BusinessInterface.OnBusinessResultListener() {
//            @Override
//            public void onBusinessResult(boolean isSuccess, Object object, String message) {
//                if(isSuccess){
//                    List<WeekTimetableInfo> list = ((List<WeekTimetableInfo>) object);
//                    if (list != null && list.size() > 0) {
//                        weekFragment.setDatas(list);
//                        List<CourseInfo> courseInfoList = new ArrayList<>();
//                        WeekTimetableInfo currentWeekInfo = list.get(0);
//                        int currentDayOfWeekIndex = DateUtil.getDayOfWeekIndex(System.currentTimeMillis());
//                        long dayMillis = DateUtil.string2Millis(currentWeekInfo.getWeekDay(), DateUtil.DATE_FORMAT_YYYY_MM_DD + " " + DateUtil.TIME_FORMAT_HH_MM_SS)
//                                + (currentDayOfWeekIndex - 1) * DateUtil.MILLIS_PER_DAY;
//                        switch (currentDayOfWeekIndex) {
//                            case 1:
//                                courseInfoList.addAll(currentWeekInfo.getD1());
//                                break;
//                            case 2:
//                                courseInfoList.addAll(currentWeekInfo.getD2());
//                                break;
//                            case 3:
//                                courseInfoList.addAll(currentWeekInfo.getD3());
//                                break;
//                            case 4:
//                                courseInfoList.addAll(currentWeekInfo.getD4());
//                                break;
//                            case 5:
//                                courseInfoList.addAll(currentWeekInfo.getD5());
//                                break;
//                            case 6:
//                                courseInfoList.addAll(currentWeekInfo.getD6());
//                                break;
//                            case 7:
//                                courseInfoList.addAll(currentWeekInfo.getD7());
//                                break;
//                        }
//                        dayFragment.setDatas(courseInfoList, dayMillis);
//                    }
//                }else{
//                    showToast(message);
//                }
//                dismissLoadingDialog();
//            }
//        };
//        int userType = UserInfoManager.getCurrentUserType();
//        String userId = UserInfoManager.getCurrentUserId();
//        BusinessInterface.ITimetableManager manager = BusinessManager.getTimetableManager();
//        Logger.e("current user type:" + userType);
//        if(userType == UserInfo.USER_TYPE_STUDENT){
//            manager.getStudentWeekTimetable(userId, listener);
//        }else if(userType == UserInfo.USER_TYPE_TEACHER){
//            manager.getTeacherWeekTimetable(userId, listener);
//        }

    }

    public void showCourseDialog(CourseInfo info, long dateMillis){
        CourseDetailDialog dialog = new CourseDetailDialog(TimetableActivity.this, info, dateMillis);
        dialog.show();
    }
}
